<template>
  <div class="login-container">
    <div class="login-form-wrap">
      <div class="login-header"></div>
      <el-form ref="form" class="login-form" :model="user" :rules="formRules">
        <el-form-item prop="mobile">
          <el-input
            v-model="user.mobile"
            placeholder="请输入手机号"
            size="medium"
            prefix-icon="el-icon-mobile"
          ></el-input>
        </el-form-item>
        <el-form-item prop="code">
          <el-input
            v-model="user.code"
            placeholder="请输入验证码"
            prefix-icon="el-icon-message"
          ></el-input>
        </el-form-item>
        <el-form-item prop="agree">
          <el-checkbox v-model="user.agree"
            >我已阅读并同意用户协议和隐私条款</el-checkbox
          >
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            class="login-bt"
            @click="onLogin"
            :loading="loginLoading"
            >登录</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { login } from '@/api/user.js'
export default {
  name: 'LoginIndex',
  data () {
    return {
      user: {
        mobile: '13911111111', //
        code: '246810', //
        agree: false
      },
      loginLoading: false,
      formRules: {
        mobile: [
          { required: true, message: '手机号不能为空', trigger: 'blur' },
          {
            pattern: /^1[3|5|7|8|9]\d{9}$/,
            message: '请输入正确的号码格式',
            trigger: 'blur'
          }
        ],
        code: [
          { required: true, message: '验证码不能为空', trigger: 'blur' },
          {
            pattern: /^\d{6}$/,
            message: '请输入正确的验证码格式',
            trigger: 'blur'
          }
        ],
        agree: [
          {
            validator: (rule, value, callback) => {
              if (value) {
                callback()
              } else {
                callback(new Error('请同意用户协议'))
              }
            },
            trigger: 'blur'
          }
        ]
      }
    }
  },
  methods: {
    onLogin () {
      // 获取表单数据
      this.$refs.form.validate((valid, err) => {
        if (!valid) {
          return
        }
        this.login()
      })
    },
    login () {
      const user = this.user
      this.loginLoading = true
      login(user)
        .then((res) => {
          this.$message({
            message: '登录成功',
            type: 'success'
          })
          this.loginLoading = false

          window.localStorage.setItem('user', JSON.stringify(res.data.data))
          // 登录成功 跳转到首页
          this.$router.push({
            name: 'home'
          })
        })
        .catch((e) => {
          this.$message.error('手机号/验证码输入错误')
          this.loginLoading = false
        })
    }
  }
}
</script>

<style lang="less" scoped>
.login-container {
  display: flex;
  flex-direction: column;
  position: fixed;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
  justify-content: center;
  align-items: center;
  background: url("./login_bg.jpg") no-repeat;
  background-size: cover;

  .login-form-wrap {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    background-color: #fff;
    padding: 30px 50px;
    .login-header {
      height: 57px;
      width: 200px;
      z-index: 10;
      background: url("./logo_index.png") no-repeat;
      background-size: contain;
    }
    .login-form {
      background-color: #fff;

      min-width: 300px;
      .login-bt {
        width: 100%;
      }
    }
  }
}
</style>
